package com.cow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cow.constant.UserStatusEnum;
import com.cow.dao.UserDao;
import com.cow.dao.UserRoleDao;
import com.cow.entity.Role;
import com.cow.entity.User;
import com.cow.entity.UserRole;
import com.cow.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @email QQ550080747
 * @date 2020/10/31 9:57
 * @description 用户业务逻辑
 */

@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {
    private static Integer CUSTOMER_ROLE_ID = 5;
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserRoleDao userRoleDao;

    @Override
    public User selectById(Integer id) {
        return userDao.selectById(id);
    }

    @Override
    public User selectByKey(String accountNumber) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        return userDao.selectOne(wrapper.eq(User::getAccountNumber, accountNumber));
    }

    @Override
    public List<Role> getRoleList(Integer userId) {
        return userDao.getRoleList(userId);
    }

    @Override
    public User selectByPhone(String telephone) {
        return userDao.selectByPhone(telephone);
    }

    @Override
    public List<User> selectAll() {
        return userDao.selectList(null);
    }

    @Override
    public Integer selectCount() {
        return userDao.selectCount(null);
    }

    @Override
    public Boolean selectUserState(String accountNumber) {
        return userDao.selectUserState(accountNumber);
    }

    @Override
    public Boolean existsWithPrimaryKey(String accountNumber) {
        return userDao.selectCount(new QueryWrapper<User>().eq("account_number", accountNumber)) > 0;
    }

    @Override
    public Boolean existsWithPrimaryPhone(String telephone) {
        return userDao.existsWithPrimaryPhone(telephone);
    }

    @Override
    public Integer selectIdByKey(String accountNumber) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.select("user_id").eq("account_number", accountNumber);
        return (Integer) userDao.selectObjs(wrapper).get(0);
    }

    @Override
    public Boolean insertData(User user) {
        user.setCreatTime(new Date());
        this.save(user);
        UserRole userRole = new UserRole();
        userRole.setUserId(user.getUserId());
        userRole.setRoleId(CUSTOMER_ROLE_ID);
        userRoleDao.insertData(userRole);
        return true;
    }

//    @Override
//    public Boolean updateById(User user) {
//        return userDao.updateById(user)>0;
//    }

    @Override
    public Boolean deleteById(Integer id) {
        return userDao.deleteById(id) > 0;
    }

    @Override
    public List<User> queryAllByStatus(UserStatusEnum status) {
        return userDao.queryAllByStatus(status);
    }
}
